<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.util.List" %>
<%@page import="java.util.ArrayList"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.io.InputStream" %>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Account</title>
	
	<link rel="stylesheet" type="text/css" href="css/stylev2.css" />
	<link rel="stylesheet" type="text/css" href="css/student-system.css" /> 
	<link rel="stylesheet" type="text/css" href="css/menubar.css" />
	<link rel="stylesheet" type="text/css" href="css/frame.css" />
	
	<style type="text/css">
		a {
	      text-decoration:none;
	   }
	</style>
	
	<script type="text/javascript">
		function add_account() {
			document.location.href = "admin_account_add.jsp";
		}
		
		function deleteButton(id) {
			var username = (document.getElementById('username' + id)).innerHTML.trim();
						
			if (confirm("คุณต้องการจะลบข้อมูล " + username) == true) {
				/* AJAX */
				var xmlhttp;
				
				if(window.XMLHttpRequest) {
					// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp = new XMLHttpRequest();
				}
				else {
					// code for IE6, IE5
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				
				xmlhttp.onreadystatechange = function() {
					if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
						var result = xmlhttp.responseText;
						
						if(result == "success") {
							window.location.reload();
							alert("สำเร็จ");
						}
						else {
							alert("ไม่สามารถลบข้อมูลได้ค่ะ");
						}
					}
				}
				
				xmlhttp.open("POST", "admin_account_delete.jsp?username="+username, true);
				xmlhttp.send();
		    } else { }
		}
	</script>
</head>
<body>

<%
	// get session
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
	
	// check session
	if (htp_session == null || htp_session.getAttribute("sUser") == null) {
	    // Forward the control to login.jsp if authentication fails or session expires
	    request.getRequestDispatcher("/login.jsp").forward(request,
	        response);
	}
	if(!"admin".equals(htp_session.getAttribute("sUser"))){%>
		<script>alert("This user dont have PERMISSION to access this zone.");</script>
		<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=system_select.jsp"> 	
<%}
		
	
	
	//set Database Connection
	String hostProps = "";
	String usernameProps  = "";
	String passwordProps  = "";
	String databaseProps = "";
	
	try {
		//get current path
		ServletContext servletContext = request.getSession().getServletContext();
		
		InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
		Properties props = new Properties();
		
		props.load(input);

		hostProps  = props.getProperty("host");
		usernameProps  = props.getProperty("username");
		passwordProps  = props.getProperty("password");
		databaseProps = props.getProperty("database");
	} catch (Exception e) { 
		out.println(e);  
	}
	
	// connect database
	Connection connect = null;		
	try {
		Class.forName("com.mysql.jdbc.Driver");
	
		connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/" + databaseProps +
				"?user=" + usernameProps  + "&password=" + passwordProps );
	
		if(connect != null){
			System.out.println("Database Connect Sucesses.");
		} else {
			System.out.println("Database Connect Failed.");	
		}

	} catch (Exception e) {
		out.println(e.getMessage());
		e.printStackTrace();
	}
	
	/* find account in database */
	List<Account> account = new ArrayList<Account>();
	
	try {			   
		ResultSet rs = connect.createStatement().executeQuery("SELECT U.username, U.status, workgroup_name, W.workgroup_id, name_th, surname_th, role_name, start_date, end_date " 
												+ " FROM user U, workgroup W, people P, full_name F, account AC, authorization AU, role R, system S " 
												+ " WHERE F.full_name_id=P.full_name_id AND P.index=AC.index AND AC.username=U.username AND U.username=AU.username "
												+ " AND AU.role_id=R.role_id AND U.workgroup_id=W.workgroup_id AND R.system_id=S.system_id AND U.remove=false "
												+ " ORDER BY U.username, role_name");
		
		while(rs.next()) {
			Account acnt = new Account();
			
			acnt.setUser(rs.getString("username"));
			acnt.setWorkgroup(rs.getString("workgroup_name"));
			acnt.setWorkgroupId(rs.getInt("workgroup_id"));
			acnt.setName(rs.getString("name_th"));
			acnt.setSurname(rs.getString("surname_th"));
			acnt.setRole(rs.getString("role_name"));
			acnt.setStartDate(rs.getString("start_date"));
			acnt.setEndDate(rs.getString("end_date"));
			acnt.setStatus(rs.getString("status"));
			
			account.add(acnt);
		}
		
	} catch (SQLException e) {
		e.printStackTrace();
	}
	
	connect.close();
%>

	<div id="page-wrap">
		<div id="inside">
				<table border="0">
					<tr style="font-size:24px">
						<td width="600" height="100" style= "background-color: #273b80;border: 0px ;padding: 0px; 0px;" >
							<div id="header_banner-2"></div>
						</td>
						
						<td width="680" height="100" style= "background-color: #273b80;border: 0px;padding: 0px; 0px;" >
							<p align="right"><label><font size="4" color="#ffffff">username : <%=strUser%>&nbsp;&nbsp;</font></label></p>
							<a href="logout.jsp"><p align="right"><label style="cursor: pointer;"><font size="3" color="#ffffff">log out &nbsp;</font></label></p></a>
						</td>
					</tr>
				</table>
				
			 <ul id="drop-nav">
		        <li><a href="admin_home.jsp">หน้าแรก</a></li>
		        
		        <li>
		            <a href="">องค์กร &nbsp; &#9662;</a>
		            <ul>
		                <li><a href="admin_department.jsp">ภาควิชา</a></li>
		                <li><a href="admin_workgroup.jsp">หน่วยงาน</a></li>
		            </ul>
		        </li>
		        <li><a href="admin_role.jsp">หน้าที่</a></li>
		        <li style="background-color: #FFFFFF; padding-top: 13px; text-align: center;  border-bottom-color: #FFFFFF; border-left-color: #FFFFFF; border-right-color: #FFFFFF">
		        	<font color="#273b80" size="4.8px" face="Tahoma">บัญชีผู้ใช้</font>
		        </li>
		        <li>
		        	<a href="">บุคคล &nbsp; &#9662;</a>
		        	<ul>
		                <li><a href="admin_lecturer.jsp">อาจารย์</a></li>
		                <li><a href="admin_employee.jsp">บุคลากร</a></li>
		            </ul>
		        </li>
		        <li><a href="admin_entrance.jsp">ระบบเข้าศึกษา</a></li>
		        <li><a href="admin_status.jsp">สถานะของนักศึกษา</a></li>
		        <li>
		        	<a href="">ตำแหน่ง &nbsp; &#9662;</a>
		        	<ul>
		                <li><a href="admin_educate_position.jsp">ตำแหน่งทางวิชาการ</a></li>
		                <li><a href="admin_management_position.jsp">ตำแหน่งบริหาร</a></li>
		            </ul>
		        </li>
		    </ul>
		    				
				<div id="main-content-ac">
					
					<br/><br/><br/><br/>
					
					<table style="border: 0px">
						<tr style="border: 0px">
							<td style="border: 0px">
								<div class="imageFrame">บัญชีผู้ใช้</div>
							</td>
							<td style="border: 0px">
								<div style="margin-top: 30px; margin-left: 20px; cursor: pointer;">
									<span title="เพิ่มข้อมูล">
										<img src="image/add_icon.png" width="40px" height="40px" onclick="add_account()">	
									</span>
								</div>
							</td>
						</tr>
					</table>
					
					<br/><br/><br/><br/>
					<center>
						<div class="CSSTableGenerator">
							<table border="1">
							  <tr style="font-size:24px">
								<td><center><strong>Username</strong></center></td>
								<td><center><strong>ชื่อ-นามสกุล</strong></center></td>
								<td><center><strong>หน่วยงาน</strong></center></td>
								<td><center><strong>หน้าที่</strong></center></td>
								<td><center><strong>เริ่มต้นการ</strong></center></td>
								<td><center><strong>สิ้นสุดการ</strong></center></td>
								<td><center><strong>สถานะ</strong></center></td>
								<td>&nbsp;</td>
								<td>&nbsp;</td>
							  </tr>							  
<%
				int accountCount = 1;
				String username_temp = "";
				for(Account acct : account) {
					if(!username_temp.equals(acct.getUser())) {
%>	
							  <tr style="font-size:20px">
							  	<form id="form<%=accountCount%>" name="form<%=accountCount%>" method="post" action="admin_account_edit.jsp">
																		
									<input type="hidden" id="username" name="username" value="<%=acct.getUser()%>" /> 
									<td id="username<%=accountCount%>" name="username<%=accountCount%>">
										<%=acct.getUser() %>
									</td>
									
									<input type="hidden" id="name" name="name" value="<%=acct.getName()%>" /> 
									<input type="hidden" id="surname" name="surname" value="<%=acct.getSurname()%>" /> 
									<td id="name<%=accountCount%>" name="name<%=accountCount%>">
										<%=acct.getName()+"  "+acct.getSurname()  %>
									</td>
									
									<input type="hidden" id="workgroup" name="workgroup" value="<%=acct.getWorkgroup()%>" />
									<input type="hidden" id="workgroupId" name="workgroupId" value="<%=acct.getWorkgroupId()%>" />
									<td id="workgroup<%=accountCount%>" name="workgroup<%=accountCount%>">
										<%=acct.getWorkgroup() %>
									</td>
									
									<td id="role<%=accountCount%>" name="role<%=accountCount%>">
										<%=acct.getRole() %>
									</td>
									
									<input type="hidden" id="startDate" name="startDate" value="<%=acct.getStartDate()%>" />
									<td id="startDate<%=accountCount%>" name="startDate<%=accountCount%>">
										<%=acct.getStartDate() %>
									</td>
									
									<input type="hidden" id="endDate" name="endDate" value="<%=acct.getEndDate()%>" />
									<td id="endDate<%=accountCount%>" name="endDate<%=accountCount%>">
				<%
										if(acct.getEndDate() == null) {
											out.print("-");
										}
										else {
											out.print(acct.getEndDate());										
										}
				%>
									</td>
									
									<input type="hidden" id="status" name="status" value="<%=acct.getStatus()%>" />
									<td id="status<%=accountCount%>" name="status<%=accountCount%>">
				<%
										if("1".equals(acct.getStatus())) {
											out.print("Active");
										}
										else {
											out.print("Inactive");										
										}
				%>
									</td>
									<td>
										<center><span title="แก้ไขข้อมูล">
											<input type="image" src="image/edit_icon.png" height="30px" width="30px" />
										</span></center>
									</td>
								</form>
									<td>
										<center><span title="ลบข้อมูล">
											<input type="image" src="image/delete_icon.png" height="30px" width="30px"  onclick="deleteButton('<%=accountCount%>')" />
										</span></center>
									</td>
							  </tr>
							  
<%
						accountCount++;
					}
					else {
						%>	
						  <tr style="font-size:20px">
								<td></td>
								<td></td>
								<td></td>								
								<td>
									<%=acct.getRole() %>
								</td>
								<td></td>
								<td></td>
								<td></td>
								<td></td>
								<td></td>
						  </tr>
						  
<%						
					}
					username_temp = acct.getUser();
				}
				
%>								  

							</table>
					
					</center>
				
				
				<br><br><br><br>

					<div style="clear: both;"></div>
						<div id="footer"></div>
		</div>		
					<div style="clear: both;"></div>		
	</div>

</body>
</html>